<template>
  <div>
    <template>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>任务中心</el-breadcrumb-item>
        <el-breadcrumb-item>任务列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable = true @clear="likeClear">
              <el-button slot="append" icon="el-icon-search" @click="getTaskList"></el-button>
            </el-input>
          </el-col>
        </el-row>
        <!-- 任务列表 -->

        <el-table :data="taskList" style="width: 100%">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="任务 ID">
                  <span>{{ props.row.id }}</span>
                </el-form-item>
                <el-form-item label="任务名称">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="任务类型">
                  <span>{{ props.row.typeClassify==1?'日常任务':'一次性任务' }}</span>
                </el-form-item>
                <el-form-item label="任务介绍">
                  <span>{{ props.row.introduce }}</span>
                </el-form-item>
                <el-form-item label="确认方式">
                  <span>{{ props.row.accomplishType==1?'主账号确认':'次级账号确认' }}</span>
                </el-form-item>
                <el-form-item label="需求等级">
                  <span>{{ props.row.prepositionLevel }}</span>
                </el-form-item>
                <el-form-item label="经验奖励">
                  <span>{{ props.row.awardLevel }}</span>
                </el-form-item>
                <el-form-item label="金币奖励">
                  <span>{{ props.row.awardMoney }}</span>
                </el-form-item>
                <el-form-item label="经验惩罚">
                  <span>{{ props.row.punishmentLevel }}</span>
                </el-form-item>
                <el-form-item label="金币惩罚">
                  <span>{{ props.row.punishmentMoney }}</span>
                </el-form-item>
                <el-form-item label="任务限时" v-if="props.row.limitTime!=null">
                  <span>{{ props.row.limitTime }}小时</span>
                </el-form-item>
                <el-form-item label="创建时间">
                  <span>
                    {{props.row.createTime}}
                  </span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="任务 ID" prop="id"></el-table-column>
          <el-table-column label="任务名称" prop="name"></el-table-column>
          <el-table-column label="任务类型">
            <template slot-scope="pro">
              <span>{{pro.row.typeClassify==1?'日常任务':'一次性任务'}}</span>
            </template>
          </el-table-column>
          <el-table-column>
            <template slot-scope="pro">
              <el-button
                type="primary"
                @click="receiveTask(pro.row.id)"
                :disabled="isReceive(pro.row.isReceive)"
              >接取任务</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->

        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[2, 5, 10]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-card>
    </template>

    <el-dialog
      title="新增任务"
      :visible.sync="receiveTaskVisible"
      width="50%"
      @close="addDialogClosed"
      :close-on-click-modal="false"
      destroy-on-close="true"
    >
      <el-form
        :model="receiveTaskInfo"
        ref="addTaskFormRef"
        :rules="addUserFormRules"
        label-width="100px"
      >
        <el-form-item label="任务名" prop="name">
          <el-input v-model="receiveTaskInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="任务类型" prop="typeClassify">
          <el-radio-group v-model="receiveTaskInfo.typeClassify" size="medium">
            <el-radio border label="1">日常任务(每日可以完成一次)</el-radio>
            <el-radio border label="2">一次性任务</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="确认方式" prop="accomplishType">
          <el-radio-group v-model="receiveTaskInfo.accomplishType" size="medium">
            <el-radio border label="1">主账号确认完成(可以设置任务失败惩罚)</el-radio>
            <el-radio border label="2">次级账号确认完成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="任务介绍" prop="introduce">
          <el-input type="textarea" v-model="receiveTaskInfo.introduce"></el-input>
        </el-form-item>
        <el-form-item label="经验奖励" prop="awardLevel">
          <el-slider v-model="receiveTaskInfo.awardLevel" show-input></el-slider>
        </el-form-item>
        <el-form-item label="金币奖励" prop="awardMoney">
          <el-slider v-model="receiveTaskInfo.awardMoney" show-input></el-slider>
        </el-form-item>
        <el-form-item label="经验惩罚" prop="punishmentLevel">
          <el-slider v-model="receiveTaskInfo.punishmentLevel" show-input></el-slider>
        </el-form-item>
        <el-form-item label="金币惩罚" prop="punishmentMoney">
          <el-slider v-model="receiveTaskInfo.punishmentMoney" show-input></el-slider>
        </el-form-item>
        <el-form-item label="接取等级要求" prop="prepositionLevel">
          <el-slider v-model="receiveTaskInfo.prepositionLevel" show-input></el-slider>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="receiveTaskVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTask">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

<script>
import { secondaryGetTaskList, receiveTask } from "@/utils/SecondaryApi";
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        // 当前页数
        pageNum: 1,
        // 每页显示多少数据
        pageSize: 10,
      },
      receiveTaskVisible: false,
      receiveTaskInfo: {
        id: "",
        name: "",
        typeClassify: "1",
        introduce: "",
        prepositionLevel: "",
        awardLevel: "",
        awardMoney: "",
        accomplishType: "1",
        punishmentMoney: "",
        punishmentLevel: "",
        createTime: "",
      },
      taskList: [],
      total: 0,
    };
  },
  created() {
    this.getTaskList();
  },
  methods: {
    likeClear(){
      this.queryInfo.pageNum = 1;
      this.queryInfo.query = '';
      this,this.getTaskList()
    },
    isReceive(flag) {
      return flag == 1;
    },
    async getTaskList() {
      const { data: res } = await secondaryGetTaskList(this.queryInfo);
      if (res.meta.status !== 200) {
        return this.$message.error("获取任务列表失败！");
      }
      this.taskList = res.data.list;
      this.total = res.data.total;
    },
    addDialogClosed() {
      this.$refs.addTaskFormRef.resetFields();
    },
    async receiveTask(value) {
      console.log(value);
      const { data: res } = await receiveTask({
        id: value,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("接取任务失败" + res.meta.message);
      }
      this.$message.success("接取任务成功！");
      this.receiveTaskVisible = false;
      this.getTaskList();
    },
    handleSizeChange(newSize) {
      console.log(newSize);
      this.queryInfo.pageSize = newSize;
      this.getTaskList();
    },
    // 监听 页码值 改变事件
    handleCurrentChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pageNum = newSize;
      this.getTaskList();
    },
  },
};
</script>